<?php

namespace App\Model\Admin;

use App\Model\BaseModel;

/**
 * 人员角色关联-模型
 * @author LZH
 * @since: 2021/08/12
 * Class UserRoleModel

 */
class UserRoleModel extends BaseModel
{
    // 设置数据表名
    protected $tableName = DB_PREFIX . "user_role";

    /**
     * 获取缓存信息
     * @param int $id 记录ID
     * @author LZH
     * @since: 2021/08/12
     */
    public function getInfoById($id)
    {
        $info = parent::getInfoById($id);
        if ($info) {
        }
        return $info;
    }

    public function getUserRoleList($userId)
    {
        $roleList = $this->field('sc_role.*')
            ->join('sc_role', 'sc_user_role.role_id=sc_role.id')
            ->where('sc_user_role.user_id', $userId)
            ->where('sc_role.status', 1)
//            ->where('sc_role.mark', 1)
            ->order('sc_role.sort', 'asc')
            ->all()->toArray(false, false);
        return $roleList;
    }

    public function softDestroyByUserId($userId)
    {
        if ($this->where('user_id', $userId)->update(['mark' => 0])) {
            return true;
        } else {
            throw new \EasySwoole\ORM\Exception\Exception('软删除失败', EXCEPTION_CODE_DELETE);
        }
    }
}
